﻿using MediatR;
using ProductIntory.Domains;
using ProductionLine.Domain.Product;
using Sale.Api.Application.Command.ClientPrice;
using Sale.Domain;
using Sale.Dtos;
using Sale.ErrorCode;
using Sale.Infrastructure.Interface;

namespace Sale.Api.Application.Handler.ClientPrice
{
    public class GetClientPriceHandler : IRequestHandler<GetClientPriceCommand, ApiPaging<ClientPriceDTO>>
    {
        private readonly IBaseRepository<ClientPriceModel> clientPriceRepo;
        private readonly IBaseRepository<ClientModel> clientRepo;
        private readonly ILogger<GetClientPriceHandler> logger;
        private readonly IBaseRepository<ProductModel> productRepo;

        public GetClientPriceHandler(IBaseRepository<ClientPriceModel> clientPriceRepo,IBaseRepository<ClientModel> clientRepo,ILogger<GetClientPriceHandler> logger,IBaseRepository<ProductModel> productRepo)
        {
            this.clientPriceRepo = clientPriceRepo;
            this.clientRepo = clientRepo;
            this.logger = logger;
            this.productRepo = productRepo;
        }
        public Task<ApiPaging<ClientPriceDTO>> Handle(GetClientPriceCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ClientPriceDTO> paging = new ApiPaging<ClientPriceDTO>();
            try
            {
                var clientPrice = clientPriceRepo.GetAll().Where(x => x.IsDel == false);
                var client = clientRepo.GetAll().Where(x => x.IsDel == false);
                var product = productRepo.GetAll();
                if (!string.IsNullOrEmpty(request.ClientName))
                {
                    client = client.Where(x => x.ClientName.Contains(request.ClientName));
                }
                var linq = from a in clientPrice
                           join b in client
                           on a.ClientId equals b.ClientId
                           join c in product
                           on a.ProductCode equals c.ProductCode
                           select new ClientPriceDTO
                           {
                               ClientPriceId = a.ClientPriceId,
                               TotalAmount = a.TotalAmount,
                               ClientId = a.ClientId,
                               ClientName = b.ClientName,
                               ClientPriceCode = a.ClientPriceCode,
                               ClientPriceRemark = a.ClientPriceRemark,
                               Numder = a.Numder,
                               Price = a.Price,
                               ProductCode=c.ProductCode,
                               ProductName=c.ProductName,
                               Specification=c.Specification,
                           };
                paging.TotalCount = linq.Count();
                paging.Data = linq.OrderBy(x => x.ClientPriceId).Skip((request.index - 1) * request.size).Take(request.size).ToList();
                paging.TotalPCount = (int)Math.Ceiling((paging.TotalCount * 1.0) / request.size);
                paging.Code = ApiEnum.OK;
                paging.Msg = "客户单价显示成功";
                logger.LogInformation(paging.Msg);
                return Task.FromResult(paging);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
